package uk.ac.lkl.lpp.ui;



//import java.awt.Color;
import java.awt.Point;
import java.awt.Rectangle;

import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;

import uk.ac.lkl.common.ui.View;
import uk.ac.lkl.lpp.model.ldo.LearningEntity;



/**
 * TODO: (Yishay) This is where the JDailog for editing a LearningEntity is created, which is 
 * odd. Also, instead of having a singleton dialog, there's one created for every entity.  
 * @author Darren
 *
 * @param <O>
 */
public class EditingLearningEntityView<O extends LearningEntity> extends View<O> {
	private static final long serialVersionUID = 1L;

    private JLabel label;
    private LearningEntity entity;

    //private JDialog dialog;

    // todo: re-use this - don't create one for each instance
    //private LearningEntityEditor learningEntityEditor;


    public EditingLearningEntityView(
                                     O entity) {
        super(entity);
        this.entity = entity;
        //learningEntityEditor = new LearningEntityEditor(entity);
        //learningEntityEditor.setBorder(BorderFactory.createEtchedBorder());

        label = new JLabel();
        add(label);
        //dialog = new JDialog();
        //dialog.setTitle("Edit " + entity.getType());
        //dialog.setResizable(false);
        // dialog.setUndecorated(true);
        updateData();
        entity.addChangeListener(new ChangeListener() {

            public void stateChanged(ChangeEvent e) {
                updateData();
            }
        });

        addMouseListeners();
    }


    public void updateView() {
        // todo: sort this out
    }


    private void updateData() {
        label.setText(getModel().getTitle());
        String description = getModel().getDescription();
        String toolTipText;
        if (description.equals(""))
            toolTipText = "<no description>";
        else
            toolTipText = description;
        setToolTipText(toolTipText);
    }


    private void addMouseListeners() {
        addMouseListener(new MouseAdapter() {

            public void mouseClicked(MouseEvent e) {
                if (e.getClickCount() < 2
                        || e.getButton() != MouseEvent.BUTTON1)
                    return;

                // todo: don't need to add this on every click
                /*
                learningEntityEditor.addActionListener(new ActionListener() {

                    public void actionPerformed(ActionEvent e) {
                        dialog.setVisible(false);
                    }
                });

                dialog.setContentPane(learningEntityEditor);
                dialog.pack();
                dialog.setModal(true);
                dialog.setLocation(location);
                dialog.setVisible(true);
                */
                Rectangle bounds = getBounds();
                Point location = new Point(bounds.x, bounds.y + bounds.height);
                SwingUtilities.convertPointToScreen(location,
                                                    EditingLearningEntityView.this.getParent());
                LearningEntityEditor.getEditorDialog(entity, location).setVisible(true);
            }
        });
    }

}
